package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.WcbgBsDetailBo;
import com.ruoyi.report_ljlm.domain.vo.WcbgBsDetailVo;
import com.ruoyi.report_ljlm.domain.WcbgBsDetail;
import com.ruoyi.report_ljlm.mapper.WcbgBsDetailMapper;
import com.ruoyi.report_ljlm.service.IWcbgBsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 贝克曼梁测定回弹弯沉试验检测报告详情Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class WcbgBsDetailServiceImpl implements IWcbgBsDetailService {

    private final WcbgBsDetailMapper baseMapper;

    /**
     * 查询贝克曼梁测定回弹弯沉试验检测报告详情
     */
    @Override
    public WcbgBsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询贝克曼梁测定回弹弯沉试验检测报告详情列表
     */
    @Override
    public TableDataInfo<WcbgBsDetailVo> queryPageList(WcbgBsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcbgBsDetail> lqw = buildQueryWrapper(bo);
        Page<WcbgBsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询贝克曼梁测定回弹弯沉试验检测报告详情列表
     */
    @Override
    public List<WcbgBsDetailVo> queryList(WcbgBsDetailBo bo) {
        LambdaQueryWrapper<WcbgBsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcbgBsDetail> buildQueryWrapper(WcbgBsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcbgBsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, WcbgBsDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getCedzh()), WcbgBsDetail::getCedzh, bo.getCedzh());
        lqw.eq(StringUtils.isNotBlank(bo.getChed()), WcbgBsDetail::getChed, bo.getChed());
        lqw.eq(bo.getLubwd() != null, WcbgBsDetail::getLubwd, bo.getLubwd());
        lqw.eq(bo.getZuochtwc() != null, WcbgBsDetail::getZuochtwc, bo.getZuochtwc());
        lqw.eq(bo.getYouchtwc() != null, WcbgBsDetail::getYouchtwc, bo.getYouchtwc());
        return lqw;
    }

    /**
     * 新增贝克曼梁测定回弹弯沉试验检测报告详情
     */
    @Override
    public Boolean insertByBo(WcbgBsDetailBo bo) {
        WcbgBsDetail add = BeanUtil.toBean(bo, WcbgBsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改贝克曼梁测定回弹弯沉试验检测报告详情
     */
    @Override
    public Boolean updateByBo(WcbgBsDetailBo bo) {
        WcbgBsDetail update = BeanUtil.toBean(bo, WcbgBsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WcbgBsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除贝克曼梁测定回弹弯沉试验检测报告详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
